/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.ifal.softhouse.bd;

import br.edu.ifal.softhouse.modelo.CadastroMaquinas;
import br.edu.ifal.softhouse.modelo.FabricaConexao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author antonio
 */
public class CadastroMaquinasBD {

    public void insereMaquina(CadastroMaquinas m) throws SQLException {
        FabricaConexao fc = new FabricaConexao();
        Connection con = fc.getConnection();
        String sql = "insert into Maquinas"
                + "(nome_maq,númmaquina,mac,idade)"
                + " values (?,?,?,?)";
        PreparedStatement stmt = con.prepareStatement(sql);
        stmt.setString(1, m.getNome_maq());
        stmt.setInt(2, m.getNúmmaquina());
        stmt.setInt(3, m.getMac());
        stmt.setInt(4, m.getIdade());
        stmt.execute();
        stmt.close();
        con.close();

    }
    public void alteraMaquina(CadastroMaquinas maquina) throws SQLException {
       FabricaConexao fc = new FabricaConexao();
        Connection con = fc.getConnection();
        String sql = "update Maquinas set NomeMaquinas=?, Númmaquina=?, Mac=?, Idade=?";
        PreparedStatement stmt = con.prepareStatement(sql);
         stmt.setString(1, maquina.getNome_maq());
        stmt.setInt(2, maquina.getNúmmaquina());
        stmt.setInt(3, maquina.getMac());
        stmt.setInt(4, maquina.getIdade());
        stmt.execute();
        stmt.close();
    }
    
    public CadastroMaquinas buscaMaquinasPeloMac(int mac) throws SQLException {
        FabricaConexao fc = new FabricaConexao();
        Connection con = fc.getConnection();
        String sql = "select *  from Maquinas where Mac=?";
        PreparedStatement stmt = con.prepareStatement(sql);
        stmt.setInt(1, mac);
        ResultSet rs = stmt.executeQuery();
        
        CadastroMaquinas maq1 = new CadastroMaquinas();
        while (rs.next()) {
            maq1.setNome_maq(rs.getString("Nome_maq"));
            maq1.setNúmmaquina(rs.getInt("Númmaquina"));
            maq1.setMac(rs.getInt("Mac"));
            maq1.setIdade(rs.getInt("Idade"));

            
        }
        rs.close();
        stmt.close();
        con.close();
        return maq1;
    }
    
    public void removeMaquina(CadastroMaquinas m) throws SQLException {
        FabricaConexao fc = new FabricaConexao();
        Connection con = fc.getConnection();
        
        PreparedStatement stmt = con.prepareStatement("delete"
                + " from Maquinas where Mac=?");
        stmt.setInt(1, m.getMac());
        stmt.execute();
        stmt.close();
        con.close();
     }


    
}
